export const MACaddress = function() {
	var Build = plus.android.importClass('android.os.Build');
	console.warn(Build.VERSION.SDK_INT)
	if (Build.VERSION.SDK_INT < 23) //android 6.0以下版本
	{
		return WifiInfoMac();
	} else if (Build.VERSION.SDK_INT == 23) //android 6.0
	{
		return ReaderMac();
	} else {
		return NetworkMac();
	}
}
const WifiInfoMac = function() {
	var Context = plus.android.importClass("android.content.Context");
	var WifiManager = plus.android.importClass("android.net.wifi.WifiManager");
	var wifiManager = plus.android.runtimeMainActivity().getSystemService(Context.WIFI_SERVICE);
	var WifiInfo = plus.android.importClass("android.net.wifi.WifiInfo");
	var wifiInfo = wifiManager.getConnectionInfo();
	return wifiInfo.getMacAddress();
}
const ReaderMac = function() {
	try {
		var BufferedReader = plus.android.importClass("java.io.BufferedReader");
		var FileReader = plus.android.importClass("java.io.FileReader");
		var file = new FileReader("/sys/class/net/wlan0/address");
		var reader = new BufferedReader(file, 256);
		var address = reader.readLine();
		reader.close();
		return address;
	} catch (error) {
		NetworkMac();
		return "02:00:00:00:00:00";
	}
}
const NetworkMac = function() {
	var NetworkInterface = plus.android.importClass("java.net.NetworkInterface");
	var networkInterface = NetworkInterface.getByName("eth1");
	if (networkInterface == null) {
		networkInterface = NetworkInterface.getByName("wlan0");
	}
	if (networkInterface == null) {
		isWifi();
		return "02:00:00:00:00:00";
	}
	var mac = networkInterface.getHardwareAddress();
	var macArr = [];
	for (var i in mac) {
		macArr.push(format(mac[i]));
	}
	return macArr.join(":");
}
const format = function(mac) {
	if (parseInt(mac) >= 0) {
		var numbder = parseInt(mac).toString(16);
		var num = numbder.length == 1 ? "0" + numbder : numbder;
		return num;
	} else {
		var numbder = parseInt(256 - Math.abs(mac)).toString(16);
		var num = numbder.length == 1 ? "0" + numbder : numbder;
		return num;
	}
}
const isWifi = function() {
	if (plus.networkinfo.getCurrentType() == plus.networkinfo.CONNECTION_WIFI) {
		return "02:00:00:00:00:00";
	} else {
		plus.nativeUI.confirm("需要打开WiFi才能获取到MAC地址,是否去打开WiFi", function(event) {
			if (event.index == 0) {
				var main = plus.android.runtimeMainActivity();
				var Intent = plus.android.importClass('android.content.Intent');
				var intent = new Intent();
				intent.setClassName("com.android.settings",
					"com.android.settings.wifi.WifiSettings");
				main.startActivity(intent);
			}
		});
	}
}